#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
typedef struct DATA
{
    /* data */
    unsigned char head_type;
    unsigned short len;
    unsigned char data[0];
}__attribute__ ((__packed__)) DATA;

int main(){
    if(access("./myfifo",F_OK)){
        int ret = mkfifo("./myfifo",0664);
        if(ret < 0){
            perror("mkfifo");
            return -1;
        }
    }

    int fd = open("./myfifo",O_RDWR);
    if(fd < 0){
        perror("open");
        return -1;
    }
    unsigned char ptr[]={"hello world"};
    DATA *sendmsg=(DATA *)malloc(sizeof(DATA)+strlen(ptr));
    memcpy(sendmsg->data,ptr,strlen(ptr));
    sendmsg->head_type=0x01;
    sendmsg->len=strlen(ptr)+sizeof(DATA);
    unsigned char *data=(unsigned char *)sendmsg;
    write(fd, data, sendmsg->len);
    printf("%d\n", sendmsg->len);
    for(int j = 0; j < sendmsg->len ; j++){
        printf("%x ",data[j]);
    }
    printf("\n");
    free(sendmsg);
    close(fd);
    return 0;
}
